{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "from htmd import *\n",
    "htmd.config(viewer='webgl')\n",
    "os.chdir('/webdata/73hboiwia98hdj209jq0/')  # Skip this command."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# System Building μ-opioid Receptor in Membrane\n",
    "by [Stefan Doerr](https://github.com/stefdoerr)\n",
    "&nbsp;\n",
    "\n",
    "**Download all the required files for the tutorial from this** [link](http://pub.htmd.org/73hboiwia98hdj209jq0/mor.tar.gz)\n",
    "\n",
    "You can watch the presentation here:\n",
    "\n",
    "[![](http://pub.htmd.org/73hboiwia98hdj209jq0/opioid_youtube.png)](https://youtu.be/DF9cHKBX19A?t=22m17s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Prepare the protein"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "prot = Molecule('mor/4dkl.pdb')\n",
    "prot.filter('protein and noh and chain B or water within 5 of (chain B and protein)')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Automatically detecting segments and assigning names to them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "prot = autoSegment(prot,'protein','P')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Building the protein"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "topos  = ['mor/ff.rtf','top/top_all36_prot.rtf','top/top_all36_lipid.rtf','top/top_water_ions.rtf']\n",
    "params = ['mor/ff.prm','par/par_all36_prot.prm','par/par_all36_lipid.prm','par/par_water_ions.prm']\n",
    "\n",
    "prot = charmm.build(prot, topo=topos, param=params, \n",
    "                    outdir='./morbuild/prot/', ionize=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "prot.reps.add(sel='segid P0', style='NewCartoon', color=1)\n",
    "prot.reps.add(sel='segid P1', style='NewCartoon', color=2)\n",
    "prot.view()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Add a sodium in the receptor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sod = Molecule('mor/sod.pdb')\n",
    "sod.set('segid','S1')\n",
    "prot.append(sod)\n",
    "prot.reps.add(sel='ions', style='VDW')\n",
    "prot.view()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Embed the protein into a membrane"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "memb = Molecule('mor/membrane80by80C36.pdb')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Center the membrane onto the protein center"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pcenter = np.mean(prot.get('coords','protein'),axis=0)\n",
    "mcenter = np.mean(memb.get('coords'),axis=0)\n",
    "memb.moveBy(pcenter-mcenter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now embed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mol = embed(prot,memb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualize the embedded system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "mol.reps.add(sel='protein', style='NewCartoon', color='Secondary Structure')\n",
    "mol.reps.add(sel='ions', style='VDW')\n",
    "mol.reps.add(sel='lipids', style='Lines')\n",
    "mol.view()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Add a ligand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "lig = Molecule('mor/QM-min.pdb') \n",
    "lig.set('segid','L');\n",
    "lcenter = np.mean(lig.get('coords'),axis=0)\n",
    "newlcenter=[random.uniform(-10, 10), random.uniform(-10, 10),  43 ]\n",
    "lig.rotateBy(uniformRandomRotation(), lcenter)\n",
    "lig.moveBy(newlcenter-lcenter)\n",
    "mol.append(lig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Put it in a water box"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "coo = mol.get('coords','noh and (lipids or protein)')\n",
    "m = np.min(coo, axis=0) + [0, 0, -5]\n",
    "M = np.max(coo, axis=0) + [0, 0, 20]\n",
    "smol = solvate(mol, minmax=np.vstack((m,M)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "smol.reps.add(sel='segid L', style='Licorice')\n",
    "smol.reps.add(sel='water', style='Lines')\n",
    "smol.view()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Build!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "molbuilt = charmm.build(smol, topo=topos, param=params, \n",
    "                        outdir='./morbuild/build/', saltconc=0.15)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Visualize built system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "molbuilt.view(sel='protein', style='NewCartoon', color='Secondary Structure', hold=True)\n",
    "molbuilt.view(sel='segid L', style='Licorice', hold=True)\n",
    "molbuilt.view(sel='lipids', style='Lines', hold=True)\n",
    "molbuilt.view(sel='ions', style='VDW', hold=True)\n",
    "molbuilt.view(sel='water', style='Lines')"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
